<%-- 
    Document   : adminPanel
    Created on : 02 11, 12, 4:47:31 PM
    Author     : pauld
--%>

<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Controller.AccountManager"%>
<%@page import="Controller.AccountManager"%>
<%@page import="Controller.UserManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
        String user = (String)session.getAttribute("user");
        String name = (String)session.getAttribute("name");
        
        //if user is is not logged in or is not an admin
        if(user == null || UserManager.isAdmin(user) != 1) {
            response.sendRedirect("../index.jsp");
        }
        //if the user is logged and is an admin in check for session integrity
        else if( (String)session.getAttribute("user") != null && UserManager.isAdmin(user) > 0 ){
        
            //check session integrity by comparing the hash of the session token
            String currSession = request.getRemoteAddr().concat( session.getId() );
            try{
                if( !UserManager.checkSessionIntegrity(currSession, session) ){
                    UserManager.logout(request);
                    response.sendRedirect("../login.jsp");
                }
            }catch(NoSuchAlgorithmException e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
            catch(UnsupportedEncodingException e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
            catch(Exception e1){
                UserManager.logout(request);
                response.sendRedirect("../login.jsp");
            }
        }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="../css/960_24_col.css" />
        <link rel="stylesheet" href="../css/reset.css" />
        <link rel="stylesheet" href="../css/text.css" />
        <link rel="stylesheet" href="../css/style.css" />
        
        <script type="text/javascript" src="../scripts/jquery.js"></script>
        <script type="text/javascript" src="../scripts/jquery-ui-1.8.18.custom.min.js"></script>
        <script type="text/javascript" src="../scripts/modalScripts.js"></script>
        <script type="text/javascript" src="../scripts/formScripts.js"></script>
        <script>
	$(function() {
		$( "#accordion" ).accordion();
	});
	</script>
        
        <title>FooBar Online Bookstore | Admin Panel</title>
    </head>
    <body>
        <div id="bar"></div>
        <!-------------------------------header here----------------------->
            <div id="header-wrap">
                <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="../index.jsp"><img src="../images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="../index.jsp">Home</a></li>
                            <li><a href="../main.jsp">Products</a>
                                <ul style="display: none;">
                                    <li><a href="../main.jsp?show=book">Books</a></li>
                                    <li><a href="../main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="../main.jsp?show=cd">CD</a></li>
                                    <li><a href="../main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="../main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <li><a href='adminPanel'>Admin Panel</a></li>
                            <li><a href="../about.jsp">About</a></li>
                            <li><a href="../contact.jsp">Contact Us</a></li>
                            <%
                            out.println("<li><form action='../logout' name='logout' method='post'>"
                                + "<a id='user-header' href='../myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button' />"
                                + "</form></li>");
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <h1>Administrator Panel</h1>
                <div id="accordion" class="grid_24">
                    <h3><a href="#">Create Manager</a></h3>
                    <div>
                        <ul id="create-manager-interface">
                            <li>
                                <form action="../createManager" method="post" class="styled-form" id="create-manager-form">
                                    <label>Username: </label><br />
                                    <input type="text" id="manager-uname" name="manager-username" class="form-inputs"/><br />
                                
                                    <label>Password: </label><br />
                                    <input type="password" id="manager-pw1" name="manager-pw1" class="form-inputs"/><br />
                                
                                    <label>Confirm Password: </label><br />
                                    <input type="password" id="manager-pw2" name="manager-pw2" class="form-inputs"/><br />
                                    
                                    <label>Type: </label><br />
                                    Finance <input type="radio" name="manager-type" value="finance" />
                                    Product <input type="radio" name="manager-type" value="product" />
                                    <br />
                                
                                    <input type="submit" value="Create" class="submit-button" />
                                </form>
                            </li>
                            <li>
                                <ul class="error-pane">
                                <%
                                try{
                                    if( request.getParameter("status").equals("1") ){
                                        out.println("<li class='important success'>The Manager Added</li>");
                                    }
                                    else if( request.getParameter("status").equals("0") ){
                                        out.println("<li class='important'>Fill Up The Form Correctly</li>");
                                    }
                                    else if( request.getParameter("status").equals("-1") ){
                                        out.println("<li class='important'>The Username is Already Taken</li>");
                                    }
                                    else if( request.getParameter("status").equals("-2") ){
                                        out.println("<li class='important'>Oooops! Something Went Wrong Please Try Again Later</li>");
                                    }
                                }
                                catch(NullPointerException e1){}
                                catch(Exception e1){}
                                %>
                                <li style="display: none;" id='manager-error-uname'>Username should be 4 to 25 characters long</li>
                                <li style="display: none;" id='manager-error-pw'>Password should be 8 to 25 characters long</li>
                                <li style="display: none;" id='manager-error-pw-same'>Password should match</li>
                                </ul>
                            </li>
                        </ul>
                    </div>
                    
                    <h3><a href="#">Locked User Accounts</a></h3>
                    <div>
                        <ul class="locked-list">
                            <%
                            //FIXME does not get locked users
                            //get all locked user accounts and print them out
                            ResultSet users = AccountManager.getLockedCustomers();

                            while( users.next() ){
                                out.println( "<li>"
                                        + "<form action='../unlockAcct' method='post'>" + users.getString(1) 
                                        + "<input type='hidden' name='acct' value='" + users.getString(1) + "' />"
                                        + "<input type='submit' value='Unlock Account' class='submit-button' /></form>"
                                        + "</li>" );
                            }
                            %>
                            </ul>
                    </div>
                            
                    <h3><a href="#">Locked Managers (Product)</a></h3>
                    <div>
                        <ul class="locked-list">
                        <%
                        //get all locked product managers
                        ResultSet pmanagers = AccountManager.getLockedProductManagers();

                        while( pmanagers.next() ){
                            out.println( "<li>"
                                    + "<form action='../unlockAcct' method='post'>" + pmanagers.getString(1)
                                    + "<input type='hidden' name='acct' value='" + pmanagers.getString(1) + "' />"
                                    + "<input type='submit' value='Unlock Account' class='submit-button' /></form>"
                                    + "</li>" );
                        }
                        %>
                        </ul>
                    </div>
                    
                    <h3><a href="#">Locked Managers (Finance)</a></h3>
                    <div>
                        <ul class="locked-list">
                        <%
                        //get all locked finance managers
                        ResultSet fmanagers = AccountManager.getLockedFinanceManagers();

                        while( fmanagers.next() ){
                            out.println( "<li>"
                                    + "<form action='../unlockAcct' method='post'>" + fmanagers.getString(1)
                                    + "<input type='hidden' name='acct' value='" + fmanagers.getString(1) + "' />"
                                    + "<input type='submit' value='Unlock Account' class='submit-button' /></form>"
                                    + "</li>" );
                        }
                        %>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <!-------------------------------footer here----------------------->
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="index.jsp">home</a></li>
                        <li><a href="contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
    </body>
</html>
